package com.ybox.wx.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "微信公众号")
@ApiSupport(order = 4)
@RequestMapping("/wx")
@RestController
@Slf4j
public class WxController {
    @Autowired
    WxMpService wxMpService;

    /**
     * 用户配置 token和url时的服务认证
     * url配置为：http://ip:port/wx
     * 如果接收不到微信服务的调用，把这个接口开大点，不要限制GET等
     */
    @GetMapping
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "服务认证")
    public String transfer(String signature, String timestamp, String nonce, String echostr) {
        log.info("接收到来自微信服务器的认证消息：signature ：{}，timestamp：{}，nonce：{}，echostr：{}", signature, timestamp, nonce
                , echostr);
        if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
            log.error("消息不合法");
            return "error";
        }
        return echostr;
    }
}